package Function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;

import Frame.PngSelectJpanel;
import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCBlock;
import utils.FCDataSingles;

public class FCBlockLibrary {
//	private Vector<FCBlock> blockList = new Vector<FCBlock>();
//	private Vector<String> blockID = new Vector<String>();
	private TreeMap<String, FCBlock> blockList=new TreeMap<>();
	private FCFileCopyToMaterial filecopy = new FCFileCopyToMaterial();
	private PngSelectJpanel psj;
	private static final String BLOCK_LIST_PATH = "./data/blocklist.vw";
	private static final String BLOCKS_PATH = "./data/block";
	private int blockallnum=0;//方块总数量
	/**
	 * 初始化方块库，读取方块的数量
	 * 建议游戏启动方使用
	 * @date 2021-11-30
	 */
	public FCBlockLibrary() {
		try {
			this.blockallnum=new File(BLOCKS_PATH).listFiles().length;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 方块数据加载函数
	 * @date 2021-11-30
	 */
	public void loadBlockData() {
		try {
			File[] f=new File(BLOCKS_PATH).listFiles();
			for(int i=0;i<blockallnum;i++) {
				FCBlock block = FCDataSingles.getReader().readBlockData(f[i].getPath());
				blockList.put(block.getBlockID(),block);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取方块实际数据量
	 * @return integer
	 * @date 2021-11-30
	 */
	public int getBlockAllNum() {
		return this.blockallnum;
	}
	/**
	 * 初始化方块数据
	 * 
	 * @param isGame
	 * @param psj
	 */
	public FCBlockLibrary(boolean isGame, PngSelectJpanel psj) {
		if (!isGame) {
			this.psj = psj;
		}
		try {
//			BufferedReader reader = new BufferedReader(new FileReader(new File(BLOCK_LIST_PATH)));
//			String blocks = reader.readLine();
//			int i = 0;
			File[] f=new File(BLOCKS_PATH).listFiles();
			blockallnum=f.length;
			for(int i=0;i<f.length;i++) {
				FCBlock block = FCDataSingles.getReader().readBlockData(f[i].getPath());
				blockList.put(block.getBlockID(),block);
//				blocks = reader.readLine();
//				if(block.getBlockID().startsWith("QiangBi")){
//					System.out.println("-----"+block.getBlockID());
//					int[][] fs = block.getBlockCoor();
//					if(fs != null){
//						for(int m = 0; m < fs.length; m++){
//							for(int n = 0; n< fs[m].length; n++){
//								System.out.println("-----------"+fs[m][n]);
//							}
//						}
//					}else {
//						System.out.println("-----3------"+block.getBlockID());
//					}
//				}
			}
//			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	public void setPSJ(PngSelectJpanel psj) {
//		this.psj=psj;
//	} 
//	public int getBlockNewID() {
//		return blockName.size();
//	}
	/**
	 * 获取方块IDlist
	 * @return Set<String>
	 */
	public Set<String> getBlockIDList(){
		return this.blockList.keySet();
	}
	// 注册方块
	public void registerBlock(boolean isnew, String ID, String type, String[] pngurl, int[][] model,
			int[][] coor, String eventID) {
		if (isnew) {
			for (int i = 0; i < pngurl.length; i++)
				pngurl[i] = filecopy.copyPngToMaterialPng(0, pngurl[i]);
			FCBlock qw = new FCBlock();
			qw.registerBlock(ID, type, pngurl, model, coor, eventID);
			blockList.put(ID,qw);
			FCDataSingles.getWriter().writeBlockData(qw);
			psj.loadBlockPngAndName(this);
		} else {
			blockList.get(ID).updataBlock(ID, type, pngurl, model, coor, eventID);
			FCDataSingles.getWriter().writeBlockData(blockList.get(ID));
		}
	}

	// 检测是否重复
	public boolean isItemRepeat(String ID) {
		Set<String> a=blockList.keySet();
		for(String i:a)if(ID.equals(i))return true;
		return false;
	}

	// ��ȡ��������
//	public int getBlockNum() {
//		return (blockName == null) ? 0 : blockName.size();
//	}

	// ��ȡ���飬���ݱ��
//	public FCBlock getBlockOfNum(int num) {
//		return blockList.get(num);
//	}

	public FCBlock getBlockOfID(String ID) {
		return blockList.get(ID);
	}
//	public String getBlockNameOfID(int num) {
//		return blockName.get(num);
//	}
}
